/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * WykladowcyFrame.java
 *
 * Created on 2011-12-30, 11:43:53
 */
package bd2;

import javax.swing.JOptionPane;
import java.util.ArrayList;

/**
 *
 * @author tolkjen
 */
public class WykladowcyFrame extends javax.swing.JFrame {
    
    private SignalReciever reciever;
    private Integer kategoria;

    /** Creates new form WykladowcyFrame */
    public WykladowcyFrame() {
        initComponents();
        kategoria = 0;
    }
    
    public final Boolean loadWykladowcy(int kat) {
        ArrayList<Object[]> data = DataStore.getInstance().getWykladowcy(kat);
        if (data != null) {
            wykladowcyTable1.clear();
            for (int i=0; i<data.size(); ++i)
                wykladowcyTable1.addRow(data.get(i));
            wykladowcyTable1.updateUI();
            
            return true;
        } else {
            return false;
        }
    }
    
    public void setKategoria(int k) {
        kategoria = k;
    }
    
    public void show(SignalReciever sr) {
        reciever = sr;
        loadWykladowcy(kategoria);
        setLocationRelativeTo(null);
        setVisible(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        wykladowcyTable1 = new bd2.WykladowcyTable();

        setTitle("Wybierz wykładowcę");

        jButton1.setText("Anuluj");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Wybierz");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jScrollPane1.setViewportView(wykladowcyTable1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(410, Short.MAX_VALUE)
                .addComponent(jButton2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton1)
                .addContainerGap())
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 548, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 355, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /* Wybierz klicked */
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
    if (wykladowcyTable1.getSelectedRowCount() > 0) {
        Integer row = wykladowcyTable1.getSelectedRow();
        String nr = (String) wykladowcyTable1.getRow(row)[0];
        String info = (String) wykladowcyTable1.getRow(row)[1] + " " + 
                (String) wykladowcyTable1.getRow(row)[2];
        reciever.signal("wykładowca", new Object[]{nr, info});
        
        setVisible(false);
    } else {
        JOptionPane.showMessageDialog(null, "Wybierz wykładowcę!",
                    "Program", JOptionPane.INFORMATION_MESSAGE);
    }
}//GEN-LAST:event_jButton2ActionPerformed

    /* Anuluj clicked */
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    setVisible(false);
}//GEN-LAST:event_jButton1ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JScrollPane jScrollPane1;
    private bd2.WykladowcyTable wykladowcyTable1;
    // End of variables declaration//GEN-END:variables
}
